from django.shortcuts import render
from rest_framework import viewsets
from  rest_framework.views import  APIView
from rest_framework.response import Response


# Create your views here.
from .serializer import RequestDataSerializer
from .models import RequestData
from .olap import highConcurrent,submitOlapSql
from .utils import base_dir,readJson
import json

class RequestDataView(viewsets.ModelViewSet):
	queryset = RequestData.objects.all()
	serializer_class = RequestDataSerializer


class GetRequestData(APIView):
	'''获取接口请求的数据并且把数据存储到文件中'''
	def get(self,request):
		queryset=RequestData.objects.all()
		serializer=RequestDataSerializer(instance=queryset,many=True)
		data=json.loads(json.dumps(serializer.data))
		print('获取出来的数据:\n',data)
		print('请求方法:'+data[0]['http_method'])
		print(data[0]['count'])
		print(data[0])
		#序列化把结果信息存储到文件中
		in_file=open(base_dir()+'/request.json','w')
		json.dump(data[0],in_file)
		return Response({'status':0,'msg':'ok'})


class SendApiView(APIView):
	def get(self,request):
		data=readJson()
		timeConsuming,throughput,rate,timeData,errorRate,sum,fails=highConcurrent(
			count=data['count'],
			method=data['http_method'],
			url=data['http_url'])
		print('耗时时间:',timeConsuming)
		return Response({'status':0,'msg':'ok'})
